#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>

void handler(int signo)
{
    std::cout << "signo: " << signo << std::endl;
    pid_t id = 0;
    while(id > 0)
    {
        id = waitpid(-1, nullptr, WNOHANG); //非阻塞轮询
        if(id > 0) 
        {
            std::cout << "pid: " << id << " collected" << std::endl;
        }
    }
}

int main()
{
    signal(SIGCHLD, handler);
    // signal(SIGCHLD, SIG_IGN); //忽略子进程回收信号，子进程不僵尸
    pid_t id = fork();
    if(id == 0)
    {
        std::cout << "child running..." << std::endl;
        exit(0);
    }

    return 0;
}